package ins;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

import org.junit.Test;
import org.lanqiao.tjut.db.DBDriverFactory;

public class Circle {
	@Test
	public void doScore() {
		//创建数组
		List<Integer> l = new ArrayList<Integer>();
		Scanner input = new Scanner(System.in);
		//输入游戏次数
		System.out.println("现在开始游戏,每次游戏得到点数最高的玩家加10分,比赛结束后统计得分高低");
		System.out.println("请输入游戏次数:");
		int a = input.nextInt();
		//给总成绩表赋初值
		Player s=new Player();
		s.doInsert();
		//按次数循环
		for (int t = 1; t <= a; t++) {
			//玩家id,姓名,第几次
			String strSQL1 = "insert into e_score(p_id,p_name,time) values(?,?,?)";
			DBDriverFactory.getDBDriverInstance().batch(strSQL1, new Object[][]
					{{ 1, "乔纳森·乔斯达", t },{ 2, "乔瑟夫·乔斯达", t },{ 3, "空条承太郎", t },
				          { 4, "东方仗助", t },{ 5, "乔鲁诺·乔巴拿", t },{ 6, "空条徐伦", t }});
			
            //一次掷骰子  
			for (int i = 1; i <= 6; i++) {
				int n1 = 1 + (int) (Math.random() * 6);
				int n2 = 1 + (int) (Math.random() * 6);
				int n3 = 1 + (int) (Math.random() * 6);
				int n = n1 + n2 + n3;
				l.add(n);
                //输出和插入结果
				System.out.println(n1 + " " + n2 + " " + n3 + " " + n + " ");
				String strSQL11 = "update e_score set num1=" + n1 + ",num2=" + n2 + ",num3=" + n3 + ",num=" + n
						+ ",p_score=0 where p_id=? and time=?";
				int j = DBDriverFactory.getDBDriverInstance().update(strSQL11, new Object[] { i,t });
				System.out.println(j > 0 ? "第"+i+"位玩家成绩输入" : "输入失败");
			}
           //找出最大数
			int max = l.get(0);
			for (int i = 0; i <= 5; i++) {
				if (max <= l.get(i)) {
					max = l.get(i);
				}
			}
			//第二次循环,找出取得最高成绩的玩家并加10分
			for (int j = 0; j <= 5; j++) {
				if (max == l.get(j)) {
					System.out.println(j + " " + l.get(j));
					//单次成绩表,按次数分别加分
					String strSQL11 = "update e_score set p_score=p_score+10 where p_id=? and time=?";
					int i = DBDriverFactory.getDBDriverInstance().update(strSQL11, new Object[] {j+1,t});
					System.out.println(i > 0 ? "给获胜玩家加分" : "加分失败");
					//总成绩表,每次得到的分数加在一起
					String strSQL111 = "update s_score set p_score=p_score+10 where p_id=?";
					int i1 = DBDriverFactory.getDBDriverInstance().update(strSQL111, new Object[] {j+1});
					System.out.println(i1 > 0 ? "给获胜玩家加分" : "加分失败");
				}
			}
			//清空列表
			l.removeAll(l);
			input.close();
		}
	}
}
